from wtforms import Form, StringField, IntegerField, PasswordField
from wtforms.validators import DataRequired, length, Email, ValidationError, EqualTo
from app.models.user import User


class RegisterForm(Form):
    nickname = StringField(validators=[DataRequired(), length(min=2, max=10, message='昵称字符2到10个')])
    email = StringField(validators=[DataRequired(), length(8, 64), Email(message='邮件不符合')])
    password = PasswordField(validators=[DataRequired(message='密码不能为空'), length(3, 32)])

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('电子邮件已经被注册')

    def validate_nickname(self, field):
        if User.query.filter_by(nickname=field.data).first():
            raise ValidationError('昵称已经被注册')


class LoginForm(Form):
    email = StringField(validators=[DataRequired(), length(8, 64), Email(message='邮件不符合')])
    password = PasswordField(validators=[DataRequired(message='密码不能为空'), length(3, 32)])


class EmailForm(Form):
    email = StringField(validators=[DataRequired(), length(8, 64), Email(message='邮件不符合')])


class ResetPasswordForm(Form):
    password1 = PasswordField(validators=[DataRequired(),
                                          length(3, 32, message='密码在3到32个字符'),
                                          EqualTo('password2', message='两次密码不一样')])
    password2 = PasswordField(validators=[DataRequired(), length(3, 32)])
